package com.aaa.dao.impl;

import com.aaa.dao.UserinfoDao;
import com.aaa.utils.BaseDao;
import com.aaa.utils.MyTools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/5/30 9:47
 * @description：用户dao接口的实现类
 * @modified By：
 * @version: 1.0
 */
public class UserinfoDaoImpl extends BaseDao implements UserinfoDao {
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 查询所有
     * @create time: 2022/5/30 9:44
     * @return List<Map<String,Object>>
     */
   /* @Override
    public List<Map<String, Object>> findAllUserInfo() {
        String sql="select * from userinfo";
        List<Map<String, Object>> mapList = super.executeQuery(sql);
        return mapList;
    }*/
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 分页查询所有
     * @param map 传入的参数集合
     * @create time: 2022/5/30 9:44
     * @return List<Map<String,Object>>
     */
    @Override
    public List<Map<String, Object>> findAllUserInfoPage(Map<String,Object> map) {
        String string = MyTools.makeSql("select * from userinfo where 1=1 ", map,false);
        System.out.println(string);
        List<Map<String, Object>> mapList = super.executeQuery(string);
        return mapList;
    }

    @Override
    public int getCount(Map<String,Object> map) {
        int count=0;
        String sql = MyTools.makeSql("select count(*) my_count from userinfo  where 1=1  ", map, true);
        List<Map<String, Object>> mapList = executeQuery(sql);
        if(null!=mapList&&mapList.size()>0){
            Object myCount = mapList.get(0).get("my_count");
            //任何对象加上字符串，都变成字符串
            count=Integer.parseInt(myCount+"");
        }
        return count;
    }

    @Override
    public boolean saveUserInfo(Map<String, Object> map) {
        String sql="insert into userinfo ( username, password, salt, deptid, avatar) values (?,?,?,?,?)";
        Boolean update = executeUpdate(sql,map.get("username"),map.get("password"),map.get("salt"),map.get("deptid"),map.get("avatar"));
        return update;
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 按照主键删除单条数据
     * @create time: 2022/6/2 10:36
     * @param map
     * @return boolean
     */
    @Override
    public boolean deleteUserInfoById(Map<String, Object> map) {
        String sql="delete from userinfo where userid in ( "+map.get("userid")+" )";
        return executeUpdate(sql);
    }


    public static void main(String[] args) {
        Map<String,String> map = new HashMap();
        map.put("name","chenjian");
        map.put("age","38");
        map.put("phone","18538062907");
        //遍历map
       /* System.out.println("========打印key=========");
        for (String key : map.keySet()) {
            System.out.println(key);
        }
        System.out.println("========打印值=========");
        for (String value : map.values()) {
            System.out.println(value);
        }
        System.out.println("========打印key和值==========");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key："+entry.getKey()+"value:"+entry.getValue());
        }*/
    }

    @Override
    public boolean checkUser(Map<String, Object> map) {
        //缓存明文密码，临时用
        String passwordTemp=map.get("searchPassword").toString();
        //1,根据用户名查询用户的所有信息，去掉密码的key
        map.remove("searchPassword");
        //根据用户名查询出所有数据
        List<Map<String,Object>> mapList=this.findAllUserInfoPage(map);
        if(mapList!=null&&mapList.size()>0){
            Map<String,Object> user=mapList.get(0);
            //2.根据 查询出的盐值和传入的明文密码加密产生新密码
            String salt=user.get("salt").toString();

            //3,校验查询出来的密文和新密码是否一致
            //获取数据库中的密文
            String passwordDateBase=user.get("password").toString();
            //根据登录页面重新加密生成的密文
            String passwordNew= MyTools.makePasswordSalt(passwordTemp,salt);
            if(passwordDateBase.equals(passwordNew)){
                return true;
            }
        }
        return false;
    }
}
